﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DomainInfoLib;
using System.Threading;

namespace APPTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        static IWebAPI WebAPI = new WanWangAPI();

        static IWebAPI WebAPI1 = new SanwaysAPI();

        static IWebAPI WebAPI2 = new CnolnicAPI();

        static IWebAPI WebAPI3 = new WhomsyAPI();

        static IWebAPI WebAPI4 = new Whoisxmlapi();

        static IWebAPI WebAPI5 = new DynadotAPI();

        IWebAPI WebAPICur;

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            WebAPICur = WebAPI;
            WebAPIThreadBatchTest();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            WebAPICur = WebAPI1;
            WebAPIThreadBatchTest();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            WebAPICur = WebAPI2;
            WebAPIThreadBatchTest();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            WebAPICur = WebAPI3;
            WebAPIThreadBatchTest();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            WebAPICur = WebAPI4;
            WebAPIThreadBatchTest();
        }

        internal virtual IWebAPI CreateIWebAPI()
        {
            // TODO: 实例化相应的具体类。
            IWebAPI target = WebAPICur;
            return target;
        }

        public void WebAPIThreadBatchTest()
        {
            j = 0;
            textBox1.Text = "";
            for (int i = 1; i <= 30; i++)
            {
                Thread t = new Thread(new ThreadStart(DoWork));
                t.IsBackground = true;

                t.Start();
            }



        }


        private void DoWork()
        {

            IWebAPI target = CreateIWebAPI(); // TODO: 初始化为适当的值
            string DomainName = GetDomainName(); // TODO: 初始化为适当的值

            bool expected = false; // TODO: 初始化为适当的值
            RegisterInfo batchResult = null;
            batchResult = BatchRun(target, DomainName);
            if (batchResult == null)
            {
                batchResult = new RegisterInfo() { Response = "没有响应", Avaliable = false };
            }
            SetUI(batchResult);


        }

        private static string GetDomainName()
        {
            return "sghsghsghsghs.com";
        }

        private void SetUI(RegisterInfo batchResult)
        {
            QueryItem item = new QueryItem() { RegisteInfo = batchResult };
            if (this.InvokeRequired)
            {
                GottenRegisteInfoHandle _myInvoke = new GottenRegisteInfoHandle(GetARegisterInfo);
                this.Invoke(_myInvoke, item);
            }
            else
            {
                GetARegisterInfo(item);
            }
        }

        int j = 0;

        void GetARegisterInfo(QueryItem item)
        {
            j++;
            textBox1.Text += item.RegisteInfo.Response + "\r\n" + "\r\n" + "---------  " + j.ToString() + "  " + item.RegisteInfo.Avaliable.Value.ToString() + "   ---------" + "\r\n" + "\r\n";
        }

        private RegisterInfo BatchRun(IWebAPI target, string DomainName)
        {
            RegisterInfo actual = new RegisterInfo();

            for (int i = 0; i < 1; i++)
            {
                actual = target.GetResponseByWebAPI(DomainName);
            }
            return actual;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            WebAPICur = WebAPI5;
           // WebAPIThreadBatchTest();

            for (int i = 0; i < 5; i++)
            {
                string paramsStr = GetParamsStr(100,i);
                DynadotAPI DynadotAPI = new DomainInfoLib.DynadotAPI();
                SetUI(DynadotAPI.GetResponseByWebAPI1(paramsStr));
            }

        }

        private string GetParamsStr(int count, int surfixNumber)
        {
            StringBuilder sbParams = new StringBuilder();

            for (int i = 0; i < count; i++)
            {
                sbParams.Append(
                    string.Format(
                    "&domain{0}={1}-----{0}.com"
                    , i.ToString()
                    , surfixNumber.ToString()
                    )
                    );
            }

            return sbParams.ToString();
        }




    }
}
